import * as cc from 'cc';
declare const CC_EDITOR
const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
/**
 * 循环缩放 效果
 * @author 神兽白泽
 * @description 交流群：415468592
 * executeInEditMode 3.x暂时不支持 tween
 * cce.Engine.enterState(cce.NeedAnimState.ANIMATION_MODE);
 * cce.Engine.exitState(cce.NeedAnimState.ANIMATION_MODE);
 */
@ccclass("ScaleLoop")
@executeInEditMode()
@playOnFocus()
export default class ScaleLoop extends cc.Component {

    @property({ displayName: '缩放开始' }) private scale_start: number = 0.9;
    @property({ displayName: '缩放结束' }) private scale_end: number = 1.1;
    @property({ displayName: '缩放时间', tooltip: '缩放1/4消耗的时间' }) private time: number = 0.1;

    @property({ displayName: '自动开始' })
    private play_on_load: boolean = true;
    /** 编辑器预览 */
    private _edit_play: boolean = false;
    @property({ displayName: '编辑器预览' })
    get edit_play(): boolean {
        if (this._edit_play && !this.edit_playing) this.play();
        this.edit_playing = this._edit_play;
        return this._edit_play;
    }
    set edit_play(v: boolean) {
        this._edit_play = v;
        if (!v) this.stop();
    }

    /** 编辑器是否正在播放 */
    private edit_playing = false;

    onLoad() {

    }

    start() {
        if (CC_EDITOR) {
            if (this.edit_play) {
                this.play();
            }
        } else {
            if (this.play_on_load) {
                this.play();
            }
        }
    }

    /**
     * 缓慢摇动
     * @param node 动画节点
     */
     public play() {
        let node = this.node;
        let scale1 = cc.tween().to(this.time, { scale: { value: new cc.Vec3(this.scale_end, this.scale_end, this.scale_end), easing: 'sineOut' } })
        let scale2 = cc.tween().to(this.time, { scale: { value: new cc.Vec3(1, 1, 1), easing: 'sineIn' } })
        let scale3 = cc.tween().to(this.time, { scale: { value: new cc.Vec3(this.scale_start, this.scale_start, this.scale_start), easing: 'sineOut' } })
        let scale4 = cc.tween().to(this.time, { scale: { value: new cc.Vec3(1, 1, 1), easing: 'sineIn' } })
        let sequence1 = cc.tween().sequence(scale1, scale2, scale3, scale4)
        cc.tween(node).then(sequence1).repeatForever().start();
    }

    /** 停止动画 */
    public stop() {
        this.node.setScale(1, 1, 1);
        cc.Tween.stopAllByTarget(this.node);
    }
}